<?php

    class import_export extends CI_Controller {

		var $povolene_typy = array(
			'sql' => array('text/x-sql', 'application/octet-stream'),
			'csv' => array('text/csv', 'application/vnd.ms-excel', 'text/comma-separated-values')
		);
        
        public function __construct() {
            parent::__construct();
            if(!$this->session->userdata('logged_in') || !$this->session->userdata('admin')){
            	redirect('SluzobneCesty');	
            }
            $this->load->model("import_export_model");
			$this->load->helper(array('form', 'url'));
			include('phpExcel/php-excel.class.php');
        }
        
        public function import($file_path) {
            $file_content = $this->load->file($file_path, true);
            $this->import_export_model->import($file_content);
        }   

        public function export() {
            $this->import_export_model->export_db();
        }

        
        /////////////////////////////////
        function index() {
			
			$this->load->view('import_export_view');
			
        }
		
		
		function _povoleny_typ($typ){
			foreach ($this->povolene_typy as $key => $obj) {
				if( in_array($typ, $obj) ){
					return TRUE;
				}
			}
			return FALSE;
		}
		
		function do_upload(){
			
			if( ($_FILES['userfile']['size'] / 1024 <= 10240) && $_FILES["userfile"]["error"] == 0 && $this->_povoleny_typ($_FILES["userfile"]["type"])) {
				
				if (file_exists("uploads/" . $_FILES["userfile"]["name"])){					
					unlink("uploads/" . $_FILES["userfile"]["name"]);					
				}
				
				move_uploaded_file($_FILES['userfile']['tmp_name'], "uploads/" . $_FILES["userfile"]["name"]);
				echo "Stored in: " . "uploads/" . $_FILES["userfile"]["name"];
				
				
				if( $_FILES['userfile']['type'] == 'text/x-sql' || $_FILES['userfile']['type'] == 'application/octet-stream' ){
					
					//bolo vybrate sql subor
					$this->import("uploads/" . $_FILES["userfile"]["name"]);
					$pageData = array(
						'file_name' => $_FILES['userfile']['name'],
						'file_size' => $_FILES['userfile']['size'] 
					);
					$this->load->view('upload_success', $pageData);
					
				} elseif ($_FILES['userfile']['type'] == 'text/csv' || $_FILES['userfile']['type'] == 'application/vnd.ms-excel' || $_FILES['userfile']['type'] == 'text/comma-separated-values') {
					
					//bolo vybrate csv subor
					
					
				}
			} else {
				
				$pageData = array(
					'error' => '<p class="error">Chyba pri upload súboru!</p>'
				);	
				$this->load->view('import_export_view', $pageData);
				return;
			}
			
			
		}
		
		function _urob_hlavicku($table){
			if($table == 'people'){
				return array('person_id','degree','degree2','name','surname','degree3','department','email','sex','sektion','chair','tel','web_addresse','is_deployed','date_of_birth');
			} else if($table == 'pricestovani' || $table == 'odcestovani'){
				return array('id','person_id','city','country','organization','date_of_arrival','date_of_departure','unique');
			} 
		}
		
		function export_excel(){
			$table_name = $_POST['table_name'];
			$xls = new Excel_XML('UTF-8','',$table_name);	
			$table = $this->import_export_model->load_table($table_name)->result();
			$data = array(
				0 => $this->_urob_hlavicku($table_name),
			);
			$row = array();
			foreach ($table as $key => $obj) {
				foreach ($obj as $key => $value) {
					array_push($row, $value);
				}	
				array_push($data, $row);	
				$row = array();
			}
			
			$xls->addArray($data);
			$xls->generateXML($table_name);		
		
		}
		


    }
    
?>